// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
let login = require("./Login.js");
var Utils = require("./Utils.js");
var interval;
var _login = new login();
const cmd = {
    MATCH_FIX_GAME_RANDOM_USER: 1,//匹配,指定游戏随机用户
    MATCH_FIX_GAME_RANDOM_USER_CANCEL: 3//取消匹配,指定游戏随机用户
}

var selfUserInfoData = { "birth": "2001-05-12", "gender": 2, "icon": "mzGameResource/avatar_default", "id": 297844432, "name": "刚吃饱的茶狐", "type": 4 };

var frientUserInfoList = [{ "birth": "2008-07-13", "gender": 2, "icon": "https://bro-res2.flyme.cn/resources/user-icon/96.jpg", "id": 4261, "name": "幸运的越", "type": 9 },
{"birth":"2001-05-12","gender":2,"icon":"https://bro-res2.flyme.cn/resources/user-icon/59.jpg","id":297081342,"name":"郁闷的炎炎","type":9},
{"birth":"2001-05-12","gender":2,"icon":"https://bro-res2.flyme.cn/resources/user-icon/27.jpg","id":297810837,"name":"香喷喷的3stone","type":9},
{"birth":"2001-05-12","gender":2,"icon":"https://bro-res2.flyme.cn/resources/user-icon/93.jpg","id":297845352,"name":"蛋疼的奶文","type":9},
{"birth":"2006-05-03","gender":2,"icon":"https://bro-res2.flyme.cn/resources/user-icon/32.jpg","id":5991,"name":"激奋的TTJIN","type":9},
{"birth":"2007-03-09","gender":2,"icon":"https://bro-res2.flyme.cn/resources/user-icon/82.jpg","id":4442,"name":"刚下班的啾啾","type":9},
{"birth":"2001-05-12","gender":1,"icon":"https://bro-res2.flyme.cn/resources/user-icon/40.jpg","id":297404255,"name":"醉醺醺的ericbing","type":9},
{"birth":"2009-04-26","gender":2,"icon":"https://bro-res2.flyme.cn/resources/user-icon/23.jpg","id":5017,"name":"幸福的柳媚","type":9},
{"birth":"2001-05-12","gender":2,"icon":"https://bro-res2.flyme.cn/resources/user-icon/67.jpg","id":297845461,"name":"吃惊的又又子","type":9},
{"birth":"2008-08-17","gender":2,"icon":"https://bro-res2.flyme.cn/resources/user-icon/4.jpg","id":374,"name":"肉墩墩的龙心","type":9},

{"birth":"2001-02-10","gender":1,"icon":"https://bro-res2.flyme.cn/resources/user-icon/1.jpg","id":1374,"name":"花暮汐","type":9},
{"birth":"2002-03-11","gender":1,"icon":"https://bro-res2.flyme.cn/resources/user-icon/2.jpg","id":2374,"name":"紫蝶翩翩","type":9},
{"birth":"2003-04-12","gender":1,"icon":"https://bro-res2.flyme.cn/resources/user-icon/3.jpg","id":3374,"name":"雨生烟","type":9},
{"birth":"2004-05-13","gender":1,"icon":"https://bro-res2.flyme.cn/resources/user-icon/5.jpg","id":4374,"name":"夏末、忧伤","type":9},
{"birth":"2005-06-14","gender":1,"icon":"https://bro-res2.flyme.cn/resources/user-icon/6.jpg","id":5374,"name":"清梦伴他","type":9},
{"birth":"2006-07-15","gender":2,"icon":"https://bro-res2.flyme.cn/resources/user-icon/7.jpg","id":6374,"name":"江湖同醉","type":9},
{"birth":"2007-08-16","gender":2,"icon":"https://bro-res2.flyme.cn/resources/user-icon/8.jpg","id":7374,"name":"水晶之恋","type":9},
{"birth":"2008-09-17","gender":2,"icon":"https://bro-res2.flyme.cn/resources/user-icon/9.jpg","id":8374,"name":"神戳先生","type":9},
{"birth":"2009-10-18","gender":2,"icon":"https://bro-res2.flyme.cn/resources/user-icon/10.jpg","id":9374,"name":"此情以殇","type":9},
{"birth":"2001-11-19","gender":2,"icon":"https://bro-res2.flyme.cn/resources/user-icon/11.jpg","id":10374,"name":"玉转乾坤","type":9},
];
const code = {
    MATCH_NOT_ENOUGH_AGAIN: 4008,
    MATCH_NOT_ENOUGH: 4006,
}

cc.Class({
    extends: cc.Component,

    properties: {
        selfNode: {
            default: null,
            type: cc.Node
        },
        friendNode: {
            default: null,
            type: cc.Node
        },

        matchNode: {
            default: null,
            type: cc.Node
        },
        timeLabel: cc.Label,
        titleLabel: cc.Label,

        matchAudio: {
            url: cc.AudioClip,
            default: null
        },
        gameId: 90000002,

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        let frameSize = cc.director.getOpenGLView().getFrameSize();
        let rate = frameSize.height / frameSize.width;
        if(rate < 16 / 9){
            var canvas = this.node.getComponent(cc.Canvas);
            canvas.fitHeight = true;
            canvas.fitWidth = false;
            cc.log("fit height");
        }
    },

    start() {
        cc.director.preloadScene("game");
        this.selfNodeScript = this.selfNode.getComponent("Self");
        this.selfNodeScript.init();
        this.friendNodeScript = this.friendNode.getComponent("Friend");
        this.friendNodeScript.init();
        this.matchTime = Math.floor((Math.random() * 5) + 3);
        this.startMatchTimer();
        this.matchNode = cc.director.getScene().getChildByName("match_node");

    },

    /**
     * 取消匹配
     */
    cancelMatch() {
        this.clear();
        cc.director.loadScene("mz_game_main"); //匹配超时，点击按钮直接退出
    },

    clear() {
        cc.log("match clear");
        this.unscheduleAllCallbacks();
    },

    onDestroy() {
        this.clear();
    },

    startMatchTimer() {
        this.schedule(() => {
            if (!this.timeTick) {
                this.timeTick = 1;
            } else {
                this.timeTick += 1;
            }
            let hour = Math.floor(this.timeTick / 60 / 60);
            let min = Math.floor(this.timeTick / 60 % 60);
            let sec = this.timeTick % 60;

            this.timeCount = `${hour > 0 ? hour > 9 ? hour : '0' + hour + ":" : ''}${min > 9 ? min : '0' + min}:${sec > 9 ? sec : '0' + sec}`;
            this.timeLabel.string = this.timeCount;

            // 等待超时，并且是固定游戏，随机玩家状态时，显示超时框
            if (this.timeTick == this.matchTime) {
                var frientIndex = Math.floor(Math.random() * frientUserInfoList.length);
                this.matchSuccess({ "code": 200, "room": 2922942, "game": 90000002, "userInfo": [selfUserInfoData, frientUserInfoList[frientIndex]] });
            }
        }, 1);
    },


    matchSuccess(data) {
        console.log("pomelo log4js--匹配成功通知...")
        this.titleLabel.string = "召唤成功";
        cc.audioEngine.play(this.matchAudio, false);
        this.unscheduleAllCallbacks();
        let selfUid = _login.getUid();
        let userInfoList = data.userInfo;
        let userInfo1 = userInfoList[0];
        let userInfo2 = userInfoList[1];
        console.log("匹配的人，userInfo2 =" + userInfo2)
        if (selfUid == userInfo1.id) {
            this.selfNodeScript.onMatchSuccess(userInfo1);
            this.friendNodeScript.onMatchSuccess(userInfo2);
            data.selfInfo = userInfo1;
            data.friendInfo = userInfo2;
        } else {
            this.friendNodeScript.onMatchSuccess(userInfo1);
            this.selfNodeScript.onMatchSuccess(userInfo2);
            data.friendInfo = userInfo1;
            data.selfInfo = userInfo2;
        }
        //使用常驻节点传递游戏数据
        setTimeout(() => {
            this.loadGame(data);
        }, 1000);
        
    },


    loadGame(data) {
        this.matchNode.getComponent("ResultData").init(data);
        cc.game.addPersistRootNode(this.matchNode);
        cc.log("first time to load addPersistRootNode data =" + data.toString());
        cc.director.loadScene("game");

    }
});

